<?php


namespace App\Models\CacheModel\zl3;


use app\common\lib\Util;
use App\Models\Api\BaseCache;
use think\Log;
use AstroTool\Cache\CacheManage;

class Zl3FortuneYearAnalysisIsNew extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl3_fortune_year_analysis_is_new';

    protected $cacheModel = 'fortune.fortune_year_analysis_is_new';

    public function getIsNewStatusCacheData(int $uid)
    {
        $data = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid)->getDataAndSet(function () use ($uid)
        {
            try
            {
                $data = $this->where(['uid' => $uid])->column('rid,type,is_new,flag', 'id');

                $resData = [];
                foreach ($data as $item)
                {
                    $resData[$item['rid']][$item['type']][$item['flag']] = ['id' => (int)$item['id'], 'is_new' => (int)$item['is_new']];
                }
                foreach ($resData as $rid => $resDatum)
                {
                    $resData[$rid] = json_encode($resDatum, JSON_UNESCAPED_UNICODE);
                }
                return $resData;
            }
            catch (\Exception $e)
            {
                Log::write('Zl3FortuneYearAnalysisIsNew model => getIsNew()异常：' . $e->getMessage() . '失败行数：' . $e->getLine(), 'error');
                return [];
            }
        });

        foreach ($data as $ridItem => $datum)
        {
            $data[$ridItem] = json_decode($datum, true);
        }

        return $data;
    }

    /**
     * 获取是否有小红点
     * @param int $uid
     * @param int $rid
     * @param int $stageyear
     * @return array
     */
    public function getIsNew(int $uid, int $rid, int $stageyear): array
    {
        $data = $this->getIsNewStatusCacheData($uid);

        $isNewData = [
            1 => [
                'status' => 1,  // 0=>默认小红点状态  1=> 强制小红点状态
                'is_new' => 0    // 0=> 不显示小红点   1=>显示小红点
            ],
            2 => [
                'status' => 0,
                'is_new' => 1
            ],
            3 => [
                'status' => 0,
                'is_new' => 1
            ]
        ]; // 初始 小红点显示状态
        if ($data)
        {
            foreach ($data as $datum)
            {
                if (isset($datum[2]))
                {
                    $isNewData[2]['is_new'] = 0;
                }
                if (isset($datum[3]))
                {
                    $isNewData[3]['is_new'] = 0;
                }
            }
        }

        if (isset($data[$rid]))
        {   // 有当前档案消除小红点的 数据
            foreach ($data[$rid] as $k => $newData)
            {
                if ($k === 2)
                {
                    if ($stageyear > 0)
                    {
                        if (isset($newData[$stageyear]))
                        {
                            $isNewData[$k] = [
                                'status' => 1,
                                'is_new' => $newData[$stageyear]['is_new']
                            ];
                        }
                    } else
                    {
                        $isNewData[$k] = [
                            'status' => 1,
                            'is_new' => 0
                        ];
                    }
                } else
                {
                    if (isset($newData[1]))
                    {
                        $isNewData[$k] = [
                            'status' => 1,
                            'is_new' => $newData[1]['is_new']
                        ];
                    }
                }
            }
        }

        $isNewData[1] = [
            'status' => 1,  // 0=>默认小红点状态  1=> 强制小红点状态
            'is_new' => 0    // 0=> 不显示小红点   1=>显示小红点
        ];

        return $isNewData;
    }

    /**
     * 设置小红点状态
     * @param int $uid
     * @param int $rid
     * @param int $type
     * @param int $flag
     * @param int $isNew
     * @throws \Exception `
     */
    public function setNewStatus(int $uid, int $rid, int $type, int $flag, int $isNew)
    {
        $newInfo = $this->getIsNewStatusCacheData($uid);
        if ($type === 1)
        {
            $defaultFlag = 1;
        } else
        {
            $defaultFlag = 0;
            foreach ($newInfo as $dataItem)
            {
                if (isset($dataItem[$type]))
                {
                    if ($type !== 2)
                    {
                        $defaultFlag = 1;
                    } else
                    {
                        $defaultFlag = $flag;
                    }
                    break;
                }
            }
        }

        $data = $this->getIsNewStatusCacheData($uid);

        if (isset($data[$rid][$type][$defaultFlag]))
        {   // 有过小红点 状态记录
            $this->where(['id' => $data[$rid][$type][$defaultFlag]['id']])->update(['is_new' => $isNew]);
        } else
        {   // 之前没有记录
            $this->insert([
                'uid' => $uid,
                'rid' => $rid,
                'type' => $type,
                'flag' => $defaultFlag,
                'is_new' => $isNew,
            ]);
        }
        // 删除小红点记录 缓存
        CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid)->rmCache();
    }
}
